/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Controllers;

import static java.awt.SystemColor.text;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author BENSON
 */
public class ControllerAdminHomepage {
    /**
     * variable and views declarations
     */
    Views.AdminHomePage adminHome; 
    ActionListener actionListener;
    Model.MainModel model;
    Connection conn=null;
    String querry;
    Statement st=null;
    TableRowSorter<TableModel> rowSorter;
    
    /**
     * Default constructor to instantiate the views and the methods
     */
    
    public ControllerAdminHomepage( ){
        adminHome = new Views.AdminHomePage();
        adminHome.setVisible(true);
        model=new Model.MainModel();
        controlCombo();
                          
    }
    /**
     * Creates columns for selected table from the combo box
     * filters the rows of the table upon search
     * @param selectedObject 
     */
    
    public void chooseTable(Object selectedObject){
    if(selectedObject.equals("PATIENTS")){
    adminHome.getTable().setColumnCount(0);
    adminHome.getTable().setRowCount(0);
    adminHome.getTable().addColumn("Patient ID");
    adminHome.getTable().addColumn(" Patient Name");
    adminHome.getTable().addColumn("Patient Email");
    adminHome.getTable().addColumn("Patient PhoneNumber");
    adminHome.getTable().addColumn("Emergency Phone");
    adminHome.getTable().addColumn("Country ");
    adminHome.getTable().addColumn("Gender");
    adminHome.getTable().addColumn("Reg Date");
    
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/hosiManagerDb?zeroDateTimeBehavior=convertToNull", "root", "");
            querry = "SELECT * FROM Patients ;";
            st = conn.prepareStatement(querry);

            ResultSet rs = st.executeQuery(querry);
            while (rs.next()) {
                Object[] temp = {rs.getString("patientNo"), rs.getString("patientName"), rs.getString("email"), rs.getString("phoneNO"),
                    rs.getString("emergencyPhoneNO"), rs.getString("country"), rs.getString("gender"), rs.getString("regDate")};
                adminHome.getTable().addRow(temp);
                rowSorter = new TableRowSorter<>(adminHome.getJTable().getModel());
                adminHome.getJTable().setRowSorter(rowSorter);

                adminHome.getSearchField().getDocument().addDocumentListener(new DocumentListener(){

            @Override
            public void insertUpdate(DocumentEvent e) {
                String text = adminHome.getSearchField().getText();
                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                String text = adminHome.getSearchField().getText();
                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

        });
            }
            st.close();
            conn.close();

        } catch (ClassNotFoundException | SQLException e) {
            JOptionPane.showMessageDialog(null, e);
        }

}

    else  if(selectedObject.equals("DOCTORS")){ 
        adminHome.getTable().setColumnCount(0);
        adminHome.getTable().setRowCount(0);
        adminHome.getTable().addColumn("Doctor ID");
        adminHome.getTable().addColumn("Doctor Name");
        adminHome.getTable().addColumn("Doctor Email");
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/hosiManagerDb?zeroDateTimeBehavior=convertToNull", "root", "");
            querry = "SELECT  dID,dName,dEmail FROM Doctors ;";
            st = conn.prepareStatement(querry);

            ResultSet rs = st.executeQuery(querry);
            while (rs.next()) {
                Object[] temp = {rs.getString("dID"), rs.getString("dName"), rs.getString("dEmail")};
                adminHome.getTable().addRow(temp);
                rowSorter = new TableRowSorter<>(adminHome.getJTable().getModel());
                adminHome.getJTable().setRowSorter(rowSorter);

                adminHome.getSearchField().getDocument().addDocumentListener(new DocumentListener(){

            @Override
            public void insertUpdate(DocumentEvent e) {
                String text = adminHome.getSearchField().getText();
                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                String text = adminHome.getSearchField().getText();
                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                throw new UnsupportedOperationException("Not supported yet."); 
            }

        });
            }
            st.close();
            conn.close();

        } catch (ClassNotFoundException | SQLException e) {
            JOptionPane.showMessageDialog(null, e);
        }
        

}
else  if(selectedObject.equals("RECEPTIONIST")){
    adminHome.getTable().setColumnCount(0);
    adminHome.getTable().setRowCount(0);
    adminHome.getTable().addColumn("Receptionist ID");
    adminHome.getTable().addColumn(" Receptionist Name");
    adminHome.getTable().addColumn("Receptionist Email");
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/hosiManagerDb?zeroDateTimeBehavior=convertToNull", "root", "");
            querry = "SELECT rID,rName,rEmail FROM Receptionists ;";
            st = conn.prepareStatement(querry);

            ResultSet rs = st.executeQuery(querry);
            while (rs.next()) {
                Object[] temp = {rs.getString("rID"), rs.getString("rName"), rs.getString("rEmail")};
                adminHome.getTable().addRow(temp);
                rowSorter = new TableRowSorter<>(adminHome.getJTable().getModel());
                adminHome.getJTable().setRowSorter(rowSorter);
    
                adminHome.getSearchField().getDocument().addDocumentListener(new DocumentListener(){

            @Override
            public void insertUpdate(DocumentEvent e) {
               String text = adminHome.getSearchField().getText();
                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                String text = adminHome.getSearchField().getText();
                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

        });
            }
            st.close();
            conn.close();

        } catch (ClassNotFoundException | SQLException e) {
            JOptionPane.showMessageDialog(null, e);
        }
    }
}
    
    /**
     * Adds itemListener to the combo box
     */  
    
    public void controlCombo(){
    adminHome.getSelectedOption().addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent arg0) {
            if(arg0.getStateChange() == ItemEvent.SELECTED){
                Object object = ((JComboBox)arg0.getSource()).getSelectedItem();
                chooseTable(object);
               
            }
        }
    });

}
    /**
     * Method to add actionListener to clicked buttons
     */
    
     public void control() {
        actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                if (actionEvent.getSource() == adminHome.getAddReceptionBtn()) {
                    ControllerAddReceptionist addR = new ControllerAddReceptionist();
                    addR.control();
                }
                if (actionEvent.getSource() == adminHome.getLogoutBtn()) {
                    ControllerMainLogin mainLog = new ControllerMainLogin();
                    adminHome.setVisible(false);
                    mainLog.control();
                }
                if (actionEvent.getSource() == adminHome.getDeleteReceptionBtn()) {
                    model.deleteSelectedReceptionist(adminHome);
                }
                if (actionEvent.getSource() == adminHome.getModifyReceptionBtn()) {
                    model.modifySelectedReceptionist(adminHome);
                }
                if (actionEvent.getSource() == adminHome.getDeleteDoctorBtn()) {
                    model.deleteSelectedDoctor(adminHome);
                }
                if (actionEvent.getSource() == adminHome.getModifyDoctorBtn()) {
                    model.modifySelectedDoctor(adminHome);
                }
                 if (actionEvent.getSource() == adminHome.getAddDoctorBtn()) {
                    ControllrerAddDoctor addD = new ControllrerAddDoctor();
                    addD.control();    
                }
                 if (actionEvent.getSource() == adminHome.getListAdminMenu()) {
                    ControllerListAdmins listAdmin = new ControllerListAdmins();
                    listAdmin.control();    
                }
                 if (actionEvent.getSource() == adminHome.getAddReceptionMenu()) {
                    ControllerAddReceptionist addR = new ControllerAddReceptionist();
                    addR.control();    
                }
                 if (actionEvent.getSource() == adminHome.getModifyReceptionMenu()) {
                    model.modifySelectedReceptionist(adminHome);    
                }
                 if (actionEvent.getSource() == adminHome.getDeleteReceptionMenu()) {
                    model.deleteSelectedReceptionist(adminHome);   
                }
                  if (actionEvent.getSource() == adminHome.getAddDoctorMenu()) {
                    ControllrerAddDoctor addD = new ControllrerAddDoctor();
                    addD.control();   
                }
                 if (actionEvent.getSource() == adminHome.getModifyDoctorMenu()) {
                    model.modifySelectedDoctor(adminHome);    
                }
                 if (actionEvent.getSource() == adminHome.getDeleteDoctorMenu()) {
                     model.deleteSelectedDoctor(adminHome);   
                }
                if (actionEvent.getSource() == adminHome.getAddAdminMenu()) {
                    ControllerAddAdmin addA= new ControllerAddAdmin();
                     addA.control();  
                }
                if (actionEvent.getSource() == adminHome.getImportReportBtn()) {
                    ControllerShowImports imports = new ControllerShowImports();
                    imports.control();
                }
            }
        };
        adminHome.getAddDoctorBtn().addActionListener(actionListener);
        adminHome.getModifyDoctorBtn().addActionListener(actionListener);
        adminHome.getDeleteDoctorBtn().addActionListener(actionListener);
        adminHome.getModifyReceptionBtn().addActionListener(actionListener);
        adminHome.getAddReceptionBtn().addActionListener(actionListener);
        adminHome.getDeleteReceptionBtn().addActionListener(actionListener);
        adminHome.getLogoutBtn().addActionListener(actionListener);
        adminHome.getListAdminMenu().addActionListener(actionListener);
        adminHome.getAddReceptionMenu().addActionListener(actionListener);
        adminHome.getModifyReceptionMenu().addActionListener(actionListener);
        adminHome.getDeleteReceptionMenu().addActionListener(actionListener);
        adminHome.getAddDoctorMenu().addActionListener(actionListener);
        adminHome.getModifyDoctorMenu().addActionListener(actionListener);
        adminHome.getDeleteDoctorMenu().addActionListener(actionListener);
        adminHome.getAddAdminMenu().addActionListener(actionListener);
        adminHome.getImportReportBtn().addActionListener(actionListener);
     }
    
}

